package logic.service;

import dao.*;
import entity.*;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import vo.*;

/**
 *
 * @author Alexander
 */
public class CalendarService implements IService {

    @Override
    public void persist(IValueObject vo, EntityManager em) {
        Calendar entity = new Calendar();
        entity.setId(((CalendarVO) vo).getId());
        if (((CalendarVO) vo).getNotes() != null) {
            entity.setNotes(toNotas(((CalendarVO) vo).getNotes(), entity));
        }
        if (((CalendarVO) vo).getProjects() != null) {
            entity.setProject(toProject(((CalendarVO) vo).getProjects(), entity));
        }
        if (((CalendarVO) vo).getSchedule() != null) {
            entity.setSchedule(toMateria(((CalendarVO) vo).getSchedule(), entity, em));
        }

        new CalendarDAO().persist(entity, em);

    }

    private List<Notes> toNotas(List<NotesVO> vo, Calendar calendario) {
        List<Notes> notas = new ArrayList<>();
        for (NotesVO n : vo) {
            Notes entity = new Notes();
            entity.setId(n.getId());
            entity.setDescription(n.getDescription());
            entity.setCalendar(calendario);
            notas.add(entity);
        }
        return notas;
    }

    private List<Project> toProject(List<ProjectVO> vo, Calendar calendario) {
        List<Project> projects = new ArrayList<>();
        for (ProjectVO p : vo) {
            Project entity = new Project();
            entity.setId(p.getId());
            entity.setName(p.getName());
            entity.setType(p.getType());
            //entity.setTasks(p.getTasks());//#####
            entity.setCalendar(calendario);
            projects.add(entity);
        }
        return projects;
    }

    private List<Subject> toMateria(List<SubjectVO> vo, Calendar calendario, EntityManager em) {
        List<Subject> materias = new ArrayList<>();
        for (SubjectVO m : vo) {
            Subject entity = new Subject();
            entity.setCode(m.getCode());
            entity.setCredits(m.getCredits());
            entity.setName(m.getName());
            entity.setCalendar(calendario);
            List<ClassGroup> groups = new ArrayList<>();
            for (GroupVO group : m.getGroups()) {
                ClassGroup g = new ClassGroup();
                g.setId(group.getId());
                g.setSuject(entity);
                g.setGroupNumber(group.getGroupNumber());
                List<ClassDay> days = new ArrayList<>();
                for (ClassDayVO day : group.getClassDays()) {
                    ClassDay d = new ClassDay();
                    d.setDay(day.getDay());
                    d.setSpan(day.getSpan());
                    d.setGroup(g);
                    d.setHour(day.getHour());
                    d.setClassRoom(day.getClassRoom());
                    Teacher teacher = (Teacher) new TeacherDAO().find(day.getTeacherId(), em);
                    teacher.getClassDays().add(d);
                    d.setTeacher(teacher);
                    days.add(d);
                }
                g.setClassDays(days);
                groups.add(g);
            }
            entity.setGroups(groups);
            materias.add(entity);
        }
        return materias;
    }

    @Override
    public IValueObject find(Object id, EntityManager em) {
        Calendar cal = (Calendar) new CalendarDAO().find(id, em);
        if (cal != null) {
            return cal.toVO();
        } else {
            return null;
        }
    }

    @Override
    public void update(IValueObject vo, EntityManager em) {
        Calendar entity = new Calendar();
        entity.setId(((CalendarVO) vo).getId());
        if (((CalendarVO) vo).getNotes() != null) {
            entity.setNotes(toNotas(((CalendarVO) vo).getNotes(), entity));
        }
        if (((CalendarVO) vo).getProjects() != null) {
            entity.setProject(toProject(((CalendarVO) vo).getProjects(), entity));
        }
        if (((CalendarVO) vo).getSchedule() != null) {
            List<Subject> subjects = toMateria(((CalendarVO) vo).getSchedule(), entity, em);
            entity.setSchedule(subjects);
        }
        entity.setStudent((Student)new StudentDAO().find(((CalendarVO) vo).getStudentId(), em));
        entity.setCreatedSchedule(((CalendarVO) vo).isCreatedSchedule());
        
        for (Subject subject : entity.getSchedule()){
            new SubjectDAO().persist(subject, em);
        }
        
        new CalendarDAO().update(entity, em);
    }

    @Override
    public void delete(Object id, EntityManager em) {
        new CalendarDAO().delete(id, em);
    }

    @Override
    public List<IValueObject> getList(EntityManager em) {
        List<IValueObject> calendarVO = new ArrayList<>();
        for (Object calendar : new CalendarDAO().getList(em)) {
            calendarVO.add(((Calendar) calendar).toVO());
        }
        return calendarVO;
    }

    public void deleteSchedule(CalendarVO calendar, EntityManager em) {
        for (SubjectVO vo : calendar.getSchedule()){
            for (GroupVO group : vo.getGroups()){
                for (ClassDayVO day : group.getClassDays()){
                    ClassDay d = new ClassDay();
                    d.setId(day.getId());
                    d.setDay(day.getDay());
                    d.setSpan(day.getSpan());
                    d.setHour(day.getHour());
                    d.setClassRoom(day.getClassRoom());
                    d.setTeacher(null);
                    new ClassDayDAO().update(d, em);
                }
            }
            new SubjectDAO().delete(vo.getCode(), em);
        }
        //calendar.setSchedule(null);
        //update(calendar, em);
    }
}
